package deployr.cli;

import static deployr.core.Files.*;

import java.io.*;

import deployr.core.*;

public class UpdateCommand extends Command {

	private ProcessFactory processFactory = new ProcessFactory();

	public UpdateCommand(Command other) {
		super(other);
	}

	public UpdateCommand(ProcessFactory processFactory) {
		super(null);
		this.processFactory = processFactory;
	}

	@Override
	protected int onRun(OutputStream output, String[] rawArgs) {
		Args args = new Args(rawArgs);
		String url = args.getParameter("u").orElse("localhost:8080/deploy");
		Path explodedPath = new Path(userDir(""), (String) args.getParameter("e").orThrow("parameter -e is required"));
		String appId = args.getParameter("a").orElse(explodedPath.getName());
		UpdateProcess updateProcess = processFactory.createUpdate(url, appId);
		updateProcess.update(explodedPath);
		return 0;
	}

	@Override
	protected boolean shouldProcess(String[] args) {
		return args.length > 0 && args[0].startsWith("update");
	}
}